/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.zhaojiu.service.impl;

import me.zhengjie.modules.zhaojiu.domain.CompanyJob;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.zhaojiu.repository.CompanyJobRepository;
import me.zhengjie.modules.zhaojiu.service.CompanyJobService;
import me.zhengjie.modules.zhaojiu.service.dto.CompanyJobDto;
import me.zhengjie.modules.zhaojiu.service.dto.CompanyJobQueryCriteria;
import me.zhengjie.modules.zhaojiu.service.mapstruct.CompanyJobMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Jiayin Wei
* @date 2021-12-09
**/
@Service
@RequiredArgsConstructor
public class CompanyJobServiceImpl implements CompanyJobService {

    private final CompanyJobRepository companyJobRepository;
    private final CompanyJobMapper companyJobMapper;

    @Override
    public Map<String,Object> queryAll(CompanyJobQueryCriteria criteria, Pageable pageable){
        Page<CompanyJob> page = companyJobRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(companyJobMapper::toDto));
    }

    @Override
    public List<CompanyJobDto> queryAll(CompanyJobQueryCriteria criteria){
        return companyJobMapper.toDto(companyJobRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CompanyJobDto findById(Long jobId) {
        CompanyJob companyJob = companyJobRepository.findById(jobId).orElseGet(CompanyJob::new);
        ValidationUtil.isNull(companyJob.getJobId(),"CompanyJob","jobId",jobId);
        return companyJobMapper.toDto(companyJob);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CompanyJobDto create(CompanyJob resources) {
        return companyJobMapper.toDto(companyJobRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CompanyJob resources) {
        CompanyJob companyJob = companyJobRepository.findById(resources.getJobId()).orElseGet(CompanyJob::new);
        ValidationUtil.isNull( companyJob.getJobId(),"CompanyJob","id",resources.getJobId());
        companyJob.copy(resources);
        companyJobRepository.save(companyJob);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long jobId : ids) {
            companyJobRepository.deleteById(jobId);
        }
    }

    @Override
    public void download(List<CompanyJobDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CompanyJobDto companyJob : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("公司ID", companyJob.getCompanyId());
            map.put("职位名称", companyJob.getName());
            map.put("职位介绍", companyJob.getBrief());
            map.put("备注", companyJob.getRemark());
            map.put("状态", companyJob.getEnabled());
            map.put("创建者", companyJob.getCreateBy());
            map.put("更新者", companyJob.getUpdateBy());
            map.put("创建日期", companyJob.getCreateTime());
            map.put("更新时间", companyJob.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
